$ontext
===================================================================================
Name: VSPDReport.gms
Function: Creates the detailed reports
Developed by: Ramu Naidoo
Last modified: 01 December 2010
===================================================================================
$offtext

*===================================================================================
*Include some settings
*===================================================================================

$include vSPDpaths.inc
$include vSPDsettings.inc

*===================================================================================
*Create combined output
*===================================================================================

SETS
FromDateTimeSystem(*)          'From date time set for system summary outputs'
o_DateTimeBus(*)               'Date and time set for bus trade period outputs'
o_Bus(*,*)                     'Set of buses for the different time period'
o_Offer(*,*)                   'Set of offers for the different time period'
o_DateTimeOffer(*)             'Date and time set for offer trade period outputs'
o_DateTimeReserve(*)           'Date and time set for reserve trade period outputs'
o_Island(*,*)                  'Island definition for trade period outputs'
FromDateTimeOffer(*)           'From date time set for offer summary outputs'
i_Offer(*)                     'Set of offers for the different summary outputs'
i_Trader(*)                    'Set of traders for the different summary outputs'
o_OfferTrader(*,*)             'Set of offers mapped to traders for the different summary outputs'
FromDateTimeTrader(*)          'From date time set for trader summary outputs'
o_Trader(*)                    'Set of traders for the trader summary outputs'
o_DateTimeNode(*)              'Date and time set for node trade period outputs'
o_Node(*,*)                    'Set of nodes for the different time period'
o_DateTimeBranch(*)            'Date and time set for branch trade period outputs'
o_Branch(*,*)                  'Set of branches for the different time period'
o_BranchFromBus_TP(*,*,*)      'Set of from buses for the different branches in each of the different time periods'
o_BranchToBus_TP(*,*,*)        'Set of to buses for the different branches in each of the different time periods'
o_DateTimeBrConstraint(*)      'Date and time set for branch constraint output'
o_BrConstraint_TP(*,*)         'Set of branch constraint for the different time periods'
o_DateTimeMNodeConstraint(*)   'Date and time set for market node constraint output'
o_MNodeConstraint_TP(*,*)      'Set of market node constraint for the different time periods'
;

alias(*,dim1), (*,dim2), (*,dim3), (*,dim4), (*,dim5);

PARAMETERS
*Summary level
o_SystemNumTradePeriods       'Number of trade periods in the system output for each input data set'
o_SystemOFV                   'System objective function value ($)'
o_SystemGen                   'System generation (MWh) for each input data set'
o_SystemLoad                  'System load (MWh) for each input data set'
o_SystemLoss                  'System loss (MWh) for each input data set'
o_SystemViolation             'System violations (MWh) for each input data set'
o_SystemFIR                   'System FIR reserves (MWh) for each input data set'
o_SystemSIR                   'System SIR reserves (MWh) for each input data set'
o_SystemEnergyRevenue         'System energy revenue ($) for each input data set'
o_SystemReserveRevenue        'System reserve revenue ($) for each input data set'
o_SystemLoadCost              'System load cost ($) for each input data set'
o_SystemLoadRevenue           'System load revenue (negative load cost) ($) for each input data set'
o_SystemSurplus               'System surplus (difference between cost and revenue) ($) for each input data set'
o_OfferNumTradePeriods        'Number of trade periods in the offer output for each input data set'
o_OfferGen(*)                 'Offer generation (MWh) for each input data set'
o_OfferFIR(*)                 'Offer FIR (MWh) for each input data set'
o_OfferSIR(*)                 'Offer SIR (MWh) for each input data set'
o_OfferGenRevenue(*)          'Offer generation revenue for each input data set ($)'
o_OfferFIRRevenue(*)          'Offer FIR revenue for each input data set ($)'
o_OfferSIRRevenue(*)          'Offer SIR revenue for each input data set ($)'
o_TraderNumTradePeriods       'Number of trade periods in the trader output for each input data set'
o_TraderGen(*)                'Trader generation (MWh) for each input data set'
o_TraderFIR(*)                'Trader FIR (MWh) for each input data set'
o_TraderSIR(*)                'Trader SIR (MWh) for each input data set'
o_TraderGenRevenue(*)         'Trader generation revenue for each input data set ($)'
o_TraderFIRRevenue(*)         'Trader FIR revenue for each input data set ($)'
o_TraderSIRRevenue(*)         'Trader SIR revenue for each input data set ($)'

*Trade period level
o_BusGeneration_TP(*,*)        'Generation per bus per time period for each input data set (MW)'
o_BusLoad_TP(*,*)              'Load per bus per time period for each input data set (MW)'
o_BusPrice_TP(*,*)             'Price per bus per time period for each input data set ($/MWh)'
o_BusRevenue_TP(*,*)           'Revenue per bus per time period for each input data set ($)'
o_BusCost_TP(*,*)              'Cost per bus per time period for each input data set ($)'
o_BusDeficit_TP(*,*)           'Bus deficit generation (MW)'
o_BusSurplus_TP(*,*)           'Bus surplus generation (MW)'
o_NodeGeneration_TP(*,*)       'Generation per node per time period for each input data set (MW)'
o_NodeLoad_TP(*,*)             'Load per node per time period for each input data set (MW)'
o_NodePrice_TP(*,*)            'Price per node per time period for each input data set ($/MWh)'
o_NodeRevenue_TP(*,*)          'Revenue per node per time period for each input data set ($)'
o_NodeCost_TP(*,*)             'Cost per node per time period for each input data set ($)'
o_NodeDeficit_TP(*,*)          'Node deficit generation (MW)'
o_NodeSurplus_TP(*,*)          'Node surplus generation (MW)'
o_OfferEnergy_TP(*,*)          'Energy per offer per time period for each input data set (MW)'
o_OfferFIR_TP(*,*)             'FI reserves per offer per time period for each input data set (MW)'
o_OfferSIR_TP(*,*)             'SI reserves per offer per time period for each input data set (MW)'
o_FIRReqd_TP(*,*)              'FIR required per island per time period for each input data set (MW)'
o_SIRReqd_TP(*,*)              'SIR required per island per time period for each input data set (MW)'
o_FIRPrice_TP(*,*)             'FIR price per island per time period for each input data set ($/MW)'
o_SIRPrice_TP(*,*)             'SIR price per island per time period for each input data set ($/MW)'
o_FIRViolation_TP(*,*)         'FIR violation per island per time period for each input data set (MW)'
o_SIRViolation_TP(*,*)         'SIR violation per island per time period for each input data set (MW)'
o_BranchFlow_TP(*,*)           'Flow on each branch per time period for each input data set (MW)'
o_BranchDynamicLoss_TP(*,*)    'Dynamic loss on each branch per time period for each input data set (MW)'
o_BranchFixedLoss_TP(*,*)      'Fixed loss on each branch per time period for each input data set (MW)'
o_BranchFromBusPrice_TP(*,*)   'Price on from bus for each branch per time period for each input data set ($/MW)'
o_BranchToBusPrice_TP(*,*)     'Price on to bus for each branch per time period for each input data set ($/MW)'
o_BranchMarginalPrice_TP(*,*)  'Marginal constraint price for each branch per time period for each input data set ($/MW)'
o_BranchTotalRentals_TP(*,*)   'Total loss and congestion rentals for each branch per time period for each input data set ($)'
o_BranchCapacity_TP(*,*)       'Branch capacity per time period for each input data set (MW)'
o_BrConstraintSense_TP(*,*)    'Branch constraint sense for each time period and each input data set'
o_BrConstraintLHS_TP(*,*)      'Branch constraint LHS for each time period and each input data set'
o_BrConstraintRHS_TP(*,*)      'Branch constraint RHS for each time period and each input data set'
o_BrConstraintPrice_TP(*,*)    'Branch constraint price for each time period and each input data set'
o_MNodeConstraintSense_TP(*,*) 'Market node constraint sense for each time period and each input data set'
o_MNodeConstraintLHS_TP(*,*)   'Market node constraint LHS for each time period and each input data set'
o_MNodeConstraintRHS_TP(*,*)   'Market node constraint RHS for each time period and each input data set'
o_MNodeConstraintPrice_TP(*,*) 'Market node constraint price for each time period and each input data set'
;

*System level
$GDXIN "%OutputPath%%runName%\%VSPDInputData%_SystemOutput.gdx"
$LOAD FromDateTimeSystem
$LOAD o_SystemNumTradePeriods o_SystemOFV o_SystemGen o_SystemLoad o_SystemLoss o_SystemViolation o_SystemFIR o_SystemSIR o_SystemEnergyRevenue
$LOAD o_SystemReserveRevenue o_SystemLoadCost o_SystemLoadRevenue o_SystemSurplus
*Close the gdx
$GDXIN

*Offer level
$GDXIN "%OutputPath%%runName%\%VSPDInputData%_OfferOutput.gdx"
$LOAD FromDateTimeOffer i_Offer i_Trader o_OfferTrader
$LOAD o_OfferNumTradePeriods o_OfferGen o_OfferFIR o_OfferSIR o_OfferGenRevenue o_OfferFIRRevenue o_OfferSIRRevenue
*Close the gdx
$GDXIN

*Trader level
$GDXIN "%OutputPath%%runName%\%VSPDInputData%_TraderOutput.gdx"
$LOAD FromDateTimeTrader o_Trader
$LOAD o_TraderNumTradePeriods o_TraderGen o_TraderFIR o_TraderSIR o_TraderGenRevenue o_TraderFIRRevenue o_TraderSIRRevenue
*Close the gdx
$GDXIN

*Read in summary data if the TradePeriodReports flag is set to true
$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_BusOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeBus o_Bus
$if %TradePeriodReports%==1 $LOAD o_BusGeneration_TP o_BusLoad_TP o_BusPrice_TP o_BusRevenue_TP o_BusCost_TP o_BusDeficit_TP o_BusSurplus_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_NodeOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeNode o_Node
$if %TradePeriodReports%==1 $LOAD o_NodeGeneration_TP o_NodeLoad_TP o_NodePrice_TP o_NodeRevenue_TP o_NodeCost_TP o_NodeDeficit_TP o_NodeSurplus_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_OfferOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeOffer o_Offer
$if %TradePeriodReports%==1 $LOAD o_OfferEnergy_TP o_OfferFIR_TP o_OfferSIR_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_ReserveOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeReserve o_Island
$if %TradePeriodReports%==1 $LOAD o_FIRReqd_TP o_SIRReqd_TP o_FIRPrice_TP o_SIRPrice_TP o_FIRViolation_TP o_SIRViolation_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_BranchOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeBranch o_Branch o_BranchFromBus_TP o_BranchToBus_TP
$if %TradePeriodReports%==1 $LOAD o_BranchFlow_TP, o_BranchDynamicLoss_TP o_BranchFixedLoss_TP o_BranchFromBusPrice_TP o_BranchToBusPrice_TP
$if %TradePeriodReports%==1 $LOAD o_BranchMarginalPrice_TP o_BranchTotalRentals_TP o_BranchCapacity_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_BrConstraintOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeBrConstraint, o_BrConstraint_TP
$if %TradePeriodReports%==1 $LOAD o_BrConstraintSense_TP, o_BrConstraintLHS_TP, o_BrConstraintRHS_TP, o_BrConstraintPrice_TP
$if %TradePeriodReports%==1 $GDXIN

$if %TradePeriodReports%==1 $GDXIN "%OutputPath%%runName%\%VSPDInputData%_MNodeConstraintOutput_TP.gdx"
$if %TradePeriodReports%==1 $LOAD o_DateTimeMNodeConstraint, o_MNodeConstraint_TP
$if %TradePeriodReports%==1 $LOAD o_MNodeConstraintSense_TP, o_MNodeConstraintLHS_TP, o_MNodeConstraintRHS_TP, o_MNodeConstraintPrice_TP
$if %TradePeriodReports%==1 $GDXIN


*Define output files
FILES
SystemResults                    / "%OutputPath%%runName%\%runName%_SystemResults.csv" /
OfferResults                     / "%OutputPath%%runName%\%runName%_OfferResults.csv" /
TraderResults                    / "%OutputPath%%runName%\%runName%_TraderResults.csv" /
BusResults_TradePeriod           / "%OutputPath%%runName%\%runName%_BusResults_TradePeriod.csv" /
NodeResults_TradePeriod          / "%OutputPath%%runName%\%runName%_NodeResults_TradePeriod.csv" /
OfferResults_TradePeriod         / "%OutputPath%%runName%\%runName%_OfferResults_TradePeriod.csv" /
ReserveResults_TradePeriod       / "%OutputPath%%runName%\%runName%_ReserveResults_TradePeriod.csv" /
BranchResults_TradePeriod        / "%OutputPath%%runName%\%runName%_BranchResults_TradePeriod.csv" /
BrCnstrResults_TradePeriod       / "%OutputPath%%runName%\%runName%_BrConstraintResults_TradePeriod.csv" /
MNodeCnstrResults_TradePeriod    / "%OutputPath%%runName%\%runName%_MNodeConstraintResults_TradePeriod.csv" /
;

*Set output file format
SystemResults.pc = 5;                    SystemResults.lw = 0;                   SystemResults.pw = 9999;                        SystemResults.ap = 1;
OfferResults.pc = 5;                     OfferResults.lw = 0;                    OfferResults.pw = 9999;                         OfferResults.ap = 1;
TraderResults.pc = 5;                    TraderResults.lw = 0;                   TraderResults.pw = 9999;                        TraderResults.ap = 1;
BusResults_TradePeriod.pc = 5;           BusResults_TradePeriod.lw = 0;          BusResults_TradePeriod.pw = 9999;               BusResults_TradePeriod.ap = 1;
NodeResults_TradePeriod.pc = 5;          NodeResults_TradePeriod.lw = 0;         NodeResults_TradePeriod.pw = 9999;              NodeResults_TradePeriod.ap = 1;
OfferResults_TradePeriod.pc = 5;         OfferResults_TradePeriod.lw = 0;        OfferResults_TradePeriod.pw = 9999;             OfferResults_TradePeriod.ap = 1;
ReserveResults_TradePeriod.pc = 5;       ReserveResults_TradePeriod.lw = 0;      ReserveResults_TradePeriod.pw = 9999;           ReserveResults_TradePeriod.ap = 1;
BranchResults_TradePeriod.pc = 5;        BranchResults_TradePeriod.lw = 0;       BranchResults_TradePeriod.pw = 9999;            BranchResults_TradePeriod.ap = 1;
BrCnstrResults_TradePeriod.pc = 5;       BrCnstrResults_TradePeriod.lw = 0;      BrCnstrResults_TradePeriod.pw = 9999;           BrCnstrResults_TradePeriod.ap = 1;
MNodeCnstrResults_TradePeriod.pc = 5;    MNodeCnstrResults_TradePeriod.lw = 0;   MNodeCnstrResults_TradePeriod.pw = 9999;        MNodeCnstrResults_TradePeriod.ap = 1;

*Write out summary reports
*System level
put SystemResults;
loop(dim2 $ FromDateTimeSystem(dim2),
  put dim2.tl, o_SystemNumTradePeriods, o_SystemOFV, o_SystemGen, o_SystemLoad, o_SystemLoss, o_SystemViolation
      o_SystemFIR, o_SystemSIR, o_SystemEnergyRevenue, o_SystemReserveRevenue, o_SystemLoadCost, o_SystemLoadRevenue, o_SystemSurplus /;
);

*Offer level
put OfferResults;
loop((dim2,dim4,dim5) $ (FromDateTimeOffer(dim2) and i_Offer(dim4) and i_Trader(dim5) and o_OfferTrader(dim4,dim5) and (o_OfferGen(dim4) or o_OfferFIR(dim4) or o_OfferSIR(dim4))),
  put dim2.tl, o_OfferNumTradePeriods, dim4.tl, dim5.tl, o_OfferGen(dim4), o_OfferFIR(dim4), o_OfferSIR(dim4), o_OfferGenRevenue(dim4)
      o_OfferFIRRevenue(dim4), o_OfferSIRRevenue(dim4) /;
);

*Trader level
put TraderResults;
loop((dim2,dim4) $ (FromDateTimeTrader(dim2) and o_Trader(dim4) and (o_TraderGen(dim4) or o_TraderFIR(dim4) or o_TraderSIR(dim4))),
  put dim2.tl, o_TraderNumTradePeriods, dim4.tl, o_TraderGen(dim4), o_TraderFIR(dim4), o_TraderSIR(dim4), o_TraderGenRevenue(dim4)
      o_TraderFIRRevenue(dim4), o_TraderSIRRevenue(dim4) /;
);

*TradePeriod reports
$if %TradePeriodReports%==0 $goto SkipTradePeriodReports

put BusResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeBus(dim2) and o_Bus(dim2,dim3)),
  put dim2.tl, dim3.tl, o_BusGeneration_TP(dim2,dim3), o_BusLoad_TP(dim2,dim3), o_BusPrice_TP(dim2,dim3), o_BusRevenue_TP(dim2,dim3), o_BusCost_TP(dim2,dim3), o_BusDeficit_TP(dim2,dim3), o_BusSurplus_TP(dim2,dim3) /;
);

put NodeResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeNode(dim2) and o_Node(dim2,dim3)),
  put dim2.tl, dim3.tl, o_NodeGeneration_TP(dim2,dim3), o_NodeLoad_TP(dim2,dim3), o_NodePrice_TP(dim2,dim3), o_NodeRevenue_TP(dim2,dim3), o_NodeCost_TP(dim2,dim3), o_NodeDeficit_TP(dim2,dim3), o_NodeSurplus_TP(dim2,dim3) /;
);

put OfferResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeOffer(dim2) and o_Offer(dim2,dim3)),
  put dim2.tl, dim3.tl, o_OfferEnergy_TP(dim2,dim3), o_OfferFIR_TP(dim2,dim3), o_OfferSIR_TP(dim2,dim3) /;
);

put ReserveResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeReserve(dim2) and o_Island(dim2,dim3)),
  put dim2.tl, dim3.tl, o_FIRReqd_TP(dim2,dim3), o_SIRReqd_TP(dim2,dim3), o_FIRPrice_TP(dim2,dim3), o_SIRPrice_TP(dim2,dim3), o_FIRViolation_TP(dim2,dim3), o_SIRViolation_TP(dim2,dim3) /;
);

put BranchResults_TradePeriod;
loop((dim2,dim3,dim4,dim5) $ (o_DateTimeBranch(dim2) and o_Branch(dim2,dim3) and o_BranchFromBus_TP(dim2,dim3,dim4) and o_BranchToBus_TP(dim2,dim3,dim5)),
  put dim2.tl, dim3.tl, dim4.tl, dim5.tl, o_BranchFlow_TP(dim2,dim3), o_BranchCapacity_TP(dim2,dim3), o_BranchDynamicLoss_TP(dim2,dim3), o_BranchFixedLoss_TP(dim2,dim3)
      o_BranchFromBusPrice_TP(dim2,dim3), o_BranchToBusPrice_TP(dim2,dim3), o_BranchMarginalPrice_TP(dim2,dim3), o_BranchTotalRentals_TP(dim2,dim3) /;
);

put BrCnstrResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeBrConstraint(dim2) and o_BrConstraint_TP(dim2,dim3)),
  put dim2.tl, dim3.tl, o_BrConstraintLHS_TP(dim2,dim3), o_BrConstraintSense_TP(dim2,dim3), o_BrConstraintRHS_TP(dim2,dim3), o_BrConstraintPrice_TP(dim2,dim3) /;
);

put MNodeCnstrResults_TradePeriod;
loop((dim2,dim3) $ (o_DateTimeMNodeConstraint(dim2) and o_MNodeConstraint_TP(dim2,dim3)),
  put dim2.tl, dim3.tl, o_MNodeConstraintLHS_TP(dim2,dim3), o_MNodeConstraintSense_TP(dim2,dim3), o_MNodeConstraintRHS_TP(dim2,dim3), o_MNodeConstraintPrice_TP(dim2,dim3) /;
);

$label SkipTradePeriodReports

